package com.cj.wisdom_display

import android.app.Activity
import android.os.Handler
import android.util.ArrayMap
import com.cj.publish.http.BaseObserver
import com.cj.wisdom_display.http.*
import com.cj.wisdom_display.utils.DeviceIdUtil
import com.cj.wisdom_display.utils.LogUtil
import com.cj.wisdom_display.utils.execute
import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.ResponseBody


/**
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 *
 * Created by yi on 2021/7/1.
 */
class MainController(val handler: Handler, val activity: Activity) {

    var weather: Weather? = null
    var parkEntity: ParkEntity? = null
    var notice: Notice? = null

    fun getWeather(city: String) {
        execute(RestClient().httpService.getWeather(city),
            object : BaseObserver<BaseResult<Weather>>(activity) {
                override fun onNext(t: BaseResult<Weather>) {
                    weather = t.data
                    weather?.apply {
                        handler.sendEmptyMessage(101)
                    }
                }

            })
    }

    fun getParkInfo() {
        execute(RestClient().httpService.getPark(),
            object : BaseObserver<BaseResult<ParkEntity>>(activity) {
                override fun onNext(t: BaseResult<ParkEntity>) {
                    parkEntity = t.data
                    parkEntity?.apply {
                        handler.sendEmptyMessage(102)
                    }
                }

            })
    }

    fun getNotice() {
        execute(RestClient().httpService.getNotice(),
            object : BaseObserver<BaseResult<Notice>>(activity) {
                override fun onNext(t: BaseResult<Notice>) {
                    notice = t.data
                    notice?.apply {
                        handler.sendEmptyMessage(103)
                    }
                }

            })
    }

    fun record() {
        val params = ArrayMap<String, String>()
        params["deviceSn"] = DeviceIdUtil.getSERIAL(activity)
        val requestBody: RequestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), Gson().toJson(params))
        execute(RestClient().httpService.record(requestBody),
            object : BaseObserver<ResponseBody>(activity) {
                override fun onNext(t: ResponseBody) {
                    LogUtil.e("一键报警成功")
                }

            })
    }

    fun environment(temperature: Int, pm25: Int, noise: Int, humidity: Int) {
        val params = ArrayMap<String, String>()
        params["deviceSn"] = DeviceIdUtil.getSERIAL(activity)
        params["humidity"] = humidity.toString()
        params["noise"] = noise.toString()
        params["pm25"] = pm25.toString()
        params["temperature"] = temperature.toString()
        val requestBody: RequestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), Gson().toJson(params))
        execute(RestClient().httpService.environment(requestBody),
            object : BaseObserver<ResponseBody>(activity) {
                override fun onNext(t: ResponseBody) {
                    LogUtil.e("环境数据上传成功")
                }

            })
    }

}